package com.jubotech.business.web.controller.user;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.jubotech.business.web.domain.CustomerInfo;
import com.jubotech.business.web.dto.PassDTO;
import com.jubotech.business.web.service.CustomerService;
import com.jubotech.framework.common.DispatchMode;
import com.jubotech.framework.common.ResultInfo;
import com.jubotech.framework.util.MD5Util;
import com.jubotech.framework.util.SessionUtil;

@Controller
@RequestMapping("/user")
public class UserController {
	@Autowired 
	private CustomerService customerService;

	@RequestMapping(value = "/", method = RequestMethod.GET)
	public ModelAndView loginindex() {
		return new ModelAndView("/user/login");
	}
	@RequestMapping(value = "/welcome", method = RequestMethod.GET)
	public ModelAndView welcome() {
		return new ModelAndView("/user/welcome");
	}
	
	@GetMapping("/index")
	public ModelAndView index() {
		return new ModelAndView("/user/index");
	}

	@RequestMapping("/sysUser/{mode}/dispatch")
	public ModelAndView dispatch(HttpServletRequest request, @PathVariable String mode) {
		Map<String, Object> model = new HashMap<>();
		model.put("mode", mode);
		if (DispatchMode.PASS_MODE.equals(mode)) {
			SessionUtil.setToken(request, MD5Util.makeToken());
			return new ModelAndView("/user/sysUser/pass", model);
		}
		return new ModelAndView("/common/error");
	}
	
  
  
	@PostMapping("/sysUser/pass")
	@ResponseBody
	public ResultInfo pass(HttpServletRequest request, PassDTO dto) {
		if (StringUtils.isBlank(dto.getOldPass())) {
			return ResultInfo.fail("旧密码不能为空");
		}
		if (StringUtils.isBlank(dto.getNewPass())) {
			return ResultInfo.fail("新密码不能为空");
		}
		CustomerInfo oldUser = SessionUtil.getUser(request);
		return customerService.updatePwd(dto.getNewPass(),dto.getOldPass(),oldUser);
	}

	
}
